﻿Option Explicit On 
Option Strict On

Imports IP.Core.IPCommon
Imports IP.Core.IPBusinessService
Imports Oracle.DataAccess.Client

''' -----------------------------------------------------------------------------
''' Project	 : IPUserService
''' Class	 : Core.IPUserService.US_Object
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' - Lớp cơ bản của mọi USObject 
''' - Chứa tất cả các phương thức chung nhất
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[csung]	2/22/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class US_Object

    Protected pm_objDS As DataSet
    Protected pm_objDR As DataRow
    Protected pm_strTableName As String
    Protected pm_objBS As New BS_Object

#Region "public interface"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Tạo mới 1 us object, có ID để fill tất cả các dữ liệu có thể của đối tựơng
    ''' </summary>
    ''' <param name="i_dc_of_us_object"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/21/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub findByID(ByVal i_dc_of_us_object As Decimal)
        Try
            Dim v_ds As DataSet = pm_objDS.Copy
            v_ds.Tables(pm_strTableName).Clear()
            Me.FillDataset(v_ds, "Where ID = " & Convert.ToString(i_dc_of_us_object))
            If v_ds.Tables(pm_strTableName).Rows.Count > 0 Then
                DataRow2Me(v_ds.Tables(pm_strTableName).Rows(0))
            Else
                Debug.Assert(False, "Nham ID a, khong co the nay dau - CSUNG")
                Dim v_e As New Exception("Data not found")
                Throw v_e
            End If
        Catch v_e As Exception
            Throw v_e
        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Điền dữ liệu vào DataSet theo chuỗi điều kiện tương ứng
    ''' Chú ý: chuỗi điều kiện i_strDieuKien phải bắt đầu bằng từ WHERE
    ''' </summary>
    ''' <param name="i_objDS"></param>
    ''' <param name="i_strDieuKien"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub FillDataset(ByVal i_objDS As DataSet, _
                ByVal i_strDieuKien As String)
        pm_objBS.FillDataset(i_objDS, pm_strTableName, i_strDieuKien)
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Điền dữ liệu vào DataSet không có điều kiện
    ''' </summary>
    ''' <param name="i_objDS"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub FillDataset(ByVal i_objDS As DataSet)
        pm_objBS.FillDataset(i_objDS, pm_strTableName, "")
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Điền dữ liệu vào DataSet theo một OracleCommand tương ứng
    ''' </summary>
    ''' <param name="i_objDS"></param>
    ''' <param name="i_selectCmd"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub FillDatasetByCommandText(ByVal i_objDS As DataSet, _
                                                ByVal i_selectCmd As OracleCommand)
        i_selectCmd.Connection = Me.get_connection()
        pm_objBS.FillDatasetByCommandText(i_objDS, i_selectCmd)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Cập nhật các thông tin từ US vào CSDL
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub Update()
        ''Tên bảng phải khác rỗng nếu không thì chết luôn
        'If pm_strTableName Is Nothing Or pm_strTableName.Length = 0 Then Debug.Assert(False)
        'Dim v_DRTemp As DataRow
        'pm_objDS.EnforceConstraints = False
        'v_DRTemp = getRowClone(pm_objDR)
        'If pm_objDS.Tables(pm_strTableName).Rows.Count >= 1 Then
        '    pm_objDS.Tables(pm_strTableName).Rows.RemoveAt(0)
        'End If
        'pm_objDS.Tables(pm_strTableName).Rows.Add(v_DRTemp)
        'pm_objBS.Update(pm_objDS, pm_strTableName)
        Debug.Assert(Not pm_objDR("ID") Is DBNull.Value, "ID bằng NULL , thì không thể update được vào bảng : " & Me.pm_strTableName)
        Debug.Assert(getID() > 0, "ID bằng 0-zero, thì không thể update được vào bảng : " & Me.pm_strTableName)
        UpdateCS() 'tạm thời dùng cái này
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' làm kiểu mới , chưa biết tại sao cách cũ chết
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/10/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Protected Overridable Sub UpdateCS()
        'Tên bảng phải khác rỗng nếu không thì chết luôn
        If pm_strTableName Is Nothing Or pm_strTableName.Length = 0 Then Debug.Assert(False)
        Dim v_DRTemp As DataRow
        pm_objDS.EnforceConstraints = False
        v_DRTemp = getRowClone(pm_objDR)
        If pm_objDS.Tables(pm_strTableName).Rows.Count >= 1 Then
            pm_objDS.Tables(pm_strTableName).Rows.RemoveAt(0)
        End If
        pm_objDS.Tables(pm_strTableName).Rows.Add(v_DRTemp)
        pm_objBS.UpdateTextCommand(pm_objDS, pm_strTableName)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Chèn các thông tin mới từ US vào CSDL
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub Insert()
        ''' TAM THOI COMMENT LAI SU DUNG LENH InsertByTextCommand
        ''Tên bảng phải khác rỗng nếu không thì chết luôn
        'If pm_strTableName Is Nothing Or pm_strTableName.Length = 0 Then Debug.Assert(False, "Tên bảng phải khác rỗng nếu không thì chết luôn")
        ''Them vao dataset
        'Dim v_DRTemp As DataRow
        'v_DRTemp = getRowClone(pm_objDR)
        'pm_objDS.EnforceConstraints = False
        'If pm_objDS.Tables(pm_strTableName).Rows.Count >= 1 Then
        '    pm_objDS.Tables(pm_strTableName).Rows.RemoveAt(0)
        'End If
        'pm_objDS.Tables(pm_strTableName).Rows.Add(v_DRTemp)
        'Me.pm_objDR.Item("ID") = pm_objBS.Insert(pm_objDS, pm_strTableName)
        InsertByTextCommand()
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Insert theo cách làm command text ở client side
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/10/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Protected Overridable Sub InsertByTextCommand()
        'Tên bảng phải khác rỗng nếu không thì chết luôn
        If pm_strTableName Is Nothing Or pm_strTableName.Length = 0 Then Debug.Assert(False, "Tên bảng phải khác rỗng nếu không thì chết luôn")
        'Them vao dataset
        Dim v_DRTemp As DataRow
        v_DRTemp = getRowClone(pm_objDR)
        pm_objDS.EnforceConstraints = False
        If pm_objDS.Tables(pm_strTableName).Rows.Count >= 1 Then
            pm_objDS.Tables(pm_strTableName).Rows.RemoveAt(0)
        End If
        pm_objDS.Tables(pm_strTableName).Rows.Add(v_DRTemp)
        Me.pm_objDR.Item("ID") = pm_objBS.InsertTextCommand(pm_objDS, pm_strTableName)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Xóa bản ghi dưới cơ sở dữ liệu theo ID
    ''' </summary>
    ''' <param name="i_dcID"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub DeleteByID(ByVal i_dcID As Decimal)
        'Tên bảng phải khác rỗng nếu không thì chết luôn
        If pm_strTableName Is Nothing Or pm_strTableName.Length = 0 Then Debug.Assert(False)
        pm_objBS.DeleteByID(pm_strTableName, i_dcID)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Copy dữ liệu từ US vào một DataRow
    ''' </summary>
    ''' <param name="i_objDR">DataRow</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub Me2DataRow(ByVal i_objDR As DataRow)
        Dim v_i As Integer
        For v_i = 0 To pm_objDR.Table.Columns.Count - 1
            i_objDR.Item(getColName(v_i)) = pm_objDR.Item(getColName(v_i))
        Next
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Truyền dữ liệu từ DataRow vào cho DataRow thành phần
    ''' </summary>
    ''' <param name="i_objDR">DataRow</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub DataRow2Me(ByVal i_objDR As DataRow)
        Dim v_i As Integer
        For v_i = 0 To pm_objDR.Table.Columns.Count - 1
            pm_objDR.Item(getColName(v_i)) = i_objDR.Item(getColName(v_i))
        Next
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Chuẩn bị ở DataBase cho việc cập nhật vào CSDL
    ''' </summary>
    ''' <remarks>
    ''' Lock object in Database
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub PrepareUpdate() ' Lock object in Database
        Dim v_b_is_updatable As Boolean = isUpdatable()
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Mở Connection tới DataBase
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Function open_connection() As OracleConnection
        Return pm_objBS.open_connection()
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Kết thúc giao dịch và đóng Connection
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub commit_close_connection()
        pm_objBS.commit_close_connection()
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' RollBack và đóng Connection
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub rollback_close_connection()
        pm_objBS.rollback_close_connection()
    End Sub

    Protected Friend Overridable Sub ExecCommand(ByVal i_cmd As OracleCommand)
        pm_objBS.exec_command(i_cmd)
    End Sub

    Public Overridable Function get_connection() As OracleConnection
        Return pm_objBS.get_connection
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Cho US dùng cùng một Connection
    ''' </summary>
    ''' <param name="i_connection">Connection</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub use_connection(ByVal i_connection As OracleConnection)
        pm_objBS.use_connection(i_connection)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Cho US dùng cùng một Connection với một US khác
    ''' </summary>
    ''' <param name="i_usObject">US_Object</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub continue_connection_of(ByVal i_usObject As US_Object)
        pm_objBS.use_connection(i_usObject.get_connection)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Đặt một SavePoint trong Connection
    ''' </summary>
    ''' <param name="i_str_save_point_name">String</param>
    ''' <remarks>
    ''' Yêu cầu US_Object phải có Connection
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub setSavePoint(ByVal i_str_save_point_name As String)
        Debug.Assert(Me.is_having_connection, "Chi duoc dat SavePoint khi US_Object co transaction - tuanqt")
        pm_objBS.set_savepoint(i_str_save_point_name)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' RollBack tới SavePoint đã đặt
    ''' </summary>
    ''' <param name="i_str_save_point_name">String</param>
    ''' <remarks>
    ''' Yêu cầu phải có Connection và phải đặt SavePoint có tên này trước đó
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub rollbackToSavePoint(ByVal i_str_save_point_name As String)
        Debug.Assert(Me.is_having_connection, "Chi duoc dat SavePoint khi US_Object co transaction - tuanqt")
        pm_objBS.rollback_to_savepoint(i_str_save_point_name)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Kiểm tra USObject có transacion chưa
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Function is_having_connection() As Boolean
        Return pm_objBS.have_connection()
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Copy dữ liệu từ Datarow thành phần của US_object này vào DataRow của US_object khác
    ''' </summary>
    ''' <param name="i_usObject">US_Object</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub CopyData2USObject(ByVal i_usObject As US_Object)
        Debug.Assert(Not i_usObject Is Nothing, "phai khoi tao doi tuong truoc khi copy data vao no - tuanqt")
        Try
            i_usObject.DataRow2Me(pm_objDR)
        Catch
            Debug.Assert(False, "Copy vao doi tuong us khac kieu roi - tuanqt")
        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Xóa tất cả các trường trong DataRow thành phần
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub ClearAllFields()
        Dim v_i As Integer
        For v_i = 0 To pm_objDS.Tables(pm_strTableName).Columns.Count - 1
            pm_objDR.Item(getColName(v_i)) = Convert.DBNull
        Next
    End Sub

    Public Overrides Function ToString() As String
        Dim v_i_col_index As Integer
        Dim v_str As String = " "
        For v_i_col_index = 0 To pm_objDR.Table.Columns.Count() - 1
            v_str &= pm_objDR.Table.Columns(v_i_col_index).ColumnName & " = " & pm_objDR(v_i_col_index).ToString()
        Next
        Return v_str
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Lấy ID của US_Object
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Function getID() As Decimal
        Return Convert.ToDecimal(pm_objDR.Item("ID"))
    End Function

    Public Overridable Sub Delete()
        Me.DeleteByID(Me.getID)
    End Sub



#End Region

#Region "PROTECTED"
    'Muc dich :Chen them mot hang vao DataSet 
    'Dau vao :la mot DataRow can chen
    'Dau ra : Mot datarow da duoc chen vao DataSet
    Protected Function getRowClone(ByVal i_objDR As DataRow) As DataRow
        Dim v_Row As DataRow = pm_objDS.Tables(pm_strTableName).NewRow()
        Dim v_i As Integer
        For v_i = 0 To pm_objDS.Tables(pm_strTableName).Columns.Count - 1
            v_Row.Item(getColName(v_i)) = i_objDR.Item(getColName(v_i))
        Next
        Return v_Row
    End Function
    'Muc dich : Lay ten cua mot Column trong mot Table.
    'Dau vao  : Chi so cua Column ma ta muon lay ten
    'Dau ra   : Ten cua Column
    Protected Function getColName(ByVal i_Index As Integer) As String
        Return pm_objDS.Tables(pm_strTableName).Columns(i_Index).ColumnName
    End Function
    'Muc dich: check xem 1 record co the update duoc khong
    '
    '
    Protected Overridable Function isUpdatable() As Boolean
        'Tên bảng phải khác rỗng nếu không thì chết luôn
        If pm_strTableName Is Nothing Or pm_strTableName.Length = 0 Then Debug.Assert(False)
        Dim v_DRTemp As DataRow

        v_DRTemp = getRowClone(pm_objDR)
        If pm_objDS.Tables(pm_strTableName).Rows.Count >= 1 Then
            pm_objDS.Tables(pm_strTableName).Rows.RemoveAt(0)
        End If
        pm_objDS.Tables(pm_strTableName).Rows.Add(v_DRTemp)
        Return pm_objBS.isUpdatable(pm_objDS, pm_strTableName)
    End Function
#End Region
End Class
